package cn.jiyun.controller;

//import cn.jiyun.common.AutoLogs;
import cn.jiyun.common.config.Result;
import cn.jiyun.pojo.Department;
import cn.jiyun.service.DepartmentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("department")
@CrossOrigin

public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/findPage")
    public Result findPage(Integer pageNum, Integer pageSize, @RequestBody Department department){
        Page page=departmentService.findPage(pageNum,pageSize,department);

        return  Result.ok(page);
    }

    @RequestMapping("/tab")
    public Result tab(Department department){
        System.out.println(department);
        List<Department> departmentList = departmentService.tab(department);

        return Result.ok(departmentList);
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Department department=departmentService.getById(id);
        return Result.ok(department);
    }
    @RequestMapping("/add")
//    @AutoLogs(value = "部门管理添加操作", loggingtype  = 0)
    public Result add(@RequestBody Department department){
        try {
            if(department.getId()==null){
                department.setPid(0);
                department.setAttribute(0);
                department.setTime(LocalDateTime.now());
                departmentService.save(department);
            }else{
                departmentService.updateById(department);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.ok("添加成功！");
    }
    @RequestMapping("/add1")
    public Result add1(@RequestBody Department department, Integer id){
        department.setPid(id);
        department.setAttribute(1);
        department.setTime(LocalDateTime.now());
        departmentService.save(department);
        return Result.ok("添加成功！");
    }
    @RequestMapping("/add2")
    public Result add2(@RequestBody Department department, Integer id){
        department.setPid(id);
        department.setAttribute(2);
        department.setTime(LocalDateTime.now());
        departmentService.save(department);
        return Result.ok("添加成功！");
    }
    @RequestMapping("/deleteById")
//    @AutoLogs(value = "部门管理删除操作", loggingtype  = 0)
    public Result deleteById(Integer id){
        try {
            departmentService.removeById(id);
            return Result.ok("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("删除失败！");
    }


}
